package com.sanjishu.enterlab.electricity.sale.controller;

import com.sanjishu.enterlab.electricity.sale.domain.*;
import com.sanjishu.enterlab.electricity.sale.service.CompanyInfoService;
import com.sanjishu.enterlab.electricity.sale.service.ContractDataService;
import com.sanjishu.enterlab.electricity.sale.service.ContractInfoService;
import com.sanjishu.enterlab.electricity.sale.service.ContractSettingService;
import com.sanjishu.enterlab.electricity.sale.shiro.ShiroAdmin;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("contractInfo")
public class ContractInfoController {

    @Autowired
    private ContractInfoService contractInfoService;
    @Autowired
    private CompanyInfoService companyInfoService;
    @Autowired
    private ContractSettingService contractSettingService;
    @Autowired
    private ContractDataService contractDataService;

    @RequestMapping(value = "show/{companyId}", method = RequestMethod.GET)
    public String showContract(@PathVariable Integer companyId, Model model) {
        Subject subject = SecurityUtils.getSubject();
        ShiroAdmin loginUser = (ShiroAdmin) subject.getPrincipal();
        List<CompanyInfo> companyInfoList = companyInfoService.findOtherCompany(loginUser.getId(), companyId);
        CompanyInfo companyInfo = companyInfoService.findById(companyId);
        DetailInfo detailInfo = companyInfoService.findByCompanyId(companyId);
        ContractInfo contractInfo = contractInfoService.findByCompanyAndTime(companyId);
        if (contractInfo != null) {
            ContractSetting contractSetting = contractSettingService.getSettingByContract(contractInfo.getId());
            model.addAttribute("contractSetting", contractSetting);
        }
        if (companyInfo != null) {
            String elecConsumption = companyInfoService.getOldElec(companyInfo.getName());
            model.addAttribute("elecConsumption", elecConsumption);
        }

        model.addAttribute("companyInfoList", companyInfoList);
        model.addAttribute("companyInfo", companyInfo);
        model.addAttribute("detailInfo", detailInfo);
        model.addAttribute("contractInfo", contractInfo);
        return "contract_info/show";
    }

    @RequestMapping(value = "show/{companyId}/pro", method = RequestMethod.GET)
    public String showContractT(@PathVariable Integer companyId, Model model) {
        model.addAttribute("from", "1");
        return showContract(companyId, model);
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Response updateContract(Integer companyId, Integer contractInfoId, Integer types, String estElecConsumption,
                                   String description, BigDecimal longAgreementRate,
                                   BigDecimal bottomPrice, BigDecimal baseRate1, String tradeCycle) throws Exception {
        ContractInfo contractInfo = contractInfoService.createContractInfo(companyId, contractInfoId, types,
                estElecConsumption, description, longAgreementRate, bottomPrice, baseRate1,
                null, null, tradeCycle);
        return Response.getSuccess(contractInfo);
    }

    @RequestMapping(value = "list/{companyId}", method = RequestMethod.GET)
    public String list(@PathVariable Integer companyId, Model model) {
        Subject subject = SecurityUtils.getSubject();
        ShiroAdmin loginUser = (ShiroAdmin) subject.getPrincipal();
        List<CompanyInfo> companyInfoList = companyInfoService.findOtherCompany(loginUser.getId(), companyId);
        CompanyInfo companyInfo = companyInfoService.findById(companyId);
        DetailInfo detailInfo = companyInfoService.findByCompanyId(companyId);
        List<ContractSetting> contractSettingList = contractSettingService.listSettingByCompany(companyId);

        model.addAttribute("contractSettingList", contractSettingList);
        model.addAttribute("companyInfoList", companyInfoList);
        model.addAttribute("companyInfo", companyInfo);
        model.addAttribute("detailInfo", detailInfo);
        return "contract_info/list";
    }


    @RequestMapping(value = "file/{companyId}", method = RequestMethod.GET)
    public String allContractFile(@PathVariable Integer companyId, Model model){
        Subject subject = SecurityUtils.getSubject();
        ShiroAdmin loginUser = (ShiroAdmin) subject.getPrincipal();
        List<CompanyInfo> companyInfoList = companyInfoService.findOtherCompany(loginUser.getId(), companyId);
        CompanyInfo companyInfo = companyInfoService.findById(companyId);
        DetailInfo detailInfo = companyInfoService.findByCompanyId(companyId);
        ContractInfo contractInfo = contractInfoService.findByCompanyAndTime(companyId);
        if (contractInfo != null) {
            HashMap<String, ContractData> contractDataMap = contractDataService.findByContractInfoId(contractInfo.getId());
            ContractSetting contractSetting = contractSettingService.getSettingByContract(contractInfo.getId());
            model.addAttribute("contractDataMap", contractDataMap);
            model.addAttribute("contractSetting", contractSetting);
        }
        model.addAttribute("contractInfo", contractInfo);
        model.addAttribute("detailInfo", detailInfo);
        model.addAttribute("companyInfo", companyInfo);
        model.addAttribute("companyInfoList", companyInfoList);
        return "contract_info/contract_file";
    }

    @RequestMapping(value = "file/{companyId}/pro", method = RequestMethod.GET)
    public String allContractFileT(@PathVariable Integer companyId, Model model) {
        model.addAttribute("from", "1");
        return allContractFile(companyId, model);
    }


}
